<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>返回最后发生的错误</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.json-last-error-msg.html">« json_last_error_msg</a></li>
      <li style="float: right;"><a href="book.simdjson.html">Simdjson »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.json.html">JSON 函数</a></li>
    <li>返回最后发生的错误</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.json-last-error" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">json_last_error</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">json_last_error</span> &mdash; <span class="dc-title">返回最后发生的错误</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.json-last-error-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>json_last_error</strong></span>(): <span class="type">int</span></div>

  <p class="para rdfs-comment">
   没有指定 <strong><code>JSON_THROW_ON_ERROR</code></strong>时，返回上一次 JSON 编码解码时发生的最后一个错误（如果有）。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.json-last-error-parameters">
  <h3 class="title">参数</h3>
  <p class="para">此函数没有参数。</p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.json-last-error-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回一个整型（integer），这个值会是以下的常量之一：
  </p>
  <table class="doctable table">
   <caption><strong>JSON 错误码</strong></caption>
   
    <thead>
     <tr>
      <th>常量</th>
      <th>含义</th>
      <th>可用性</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td><strong><code>JSON_ERROR_NONE</code></strong></td>
      <td>没有错误发生</td>
      <td class="empty">&nbsp;</td>
     </tr>

     <tr>
      <td><strong><code>JSON_ERROR_DEPTH</code></strong></td>
      <td>到达了最大堆栈深度</td>
      <td class="empty">&nbsp;</td>
     </tr>

     <tr>
      <td><strong><code>JSON_ERROR_STATE_MISMATCH</code></strong></td>
      <td>无效或异常的 JSON</td>
      <td class="empty">&nbsp;</td>
     </tr>

     <tr>
      <td><strong><code>JSON_ERROR_CTRL_CHAR</code></strong></td>
      <td>控制字符错误，可能是编码不对</td>
      <td class="empty">&nbsp;</td>
     </tr>

     <tr>
      <td><strong><code>JSON_ERROR_SYNTAX</code></strong></td>
      <td>语法错误</td>
      <td class="empty">&nbsp;</td>
     </tr>

     <tr>
      <td><strong><code>JSON_ERROR_UTF8</code></strong></td>
      <td>异常的 UTF-8 字符，也许是因为不正确的编码。</td>
      <td class="empty">&nbsp;</td>
     </tr>

     <tr>
      <td><strong><code>JSON_ERROR_RECURSION</code></strong></td>
      <td>One or more recursive references in the value to be encoded</td>
      <td class="empty">&nbsp;</td>
     </tr>

     <tr>
      <td><strong><code>JSON_ERROR_INF_OR_NAN</code></strong></td>
      <td>
       One or more
       <a href="language.types.float.html#language.types.float.nan" class="link"><strong><code>NAN</code></strong></a>
       or <a href="function.is-infinite.html" class="link"><strong><code>INF</code></strong></a>
       values in the value to be encoded
      </td>
      <td class="empty">&nbsp;</td>
     </tr>

     <tr>
      <td><strong><code>JSON_ERROR_UNSUPPORTED_TYPE</code></strong></td>
      <td>指定的类型，值无法编码。</td>
      <td class="empty">&nbsp;</td>
     </tr>

     <tr>
      <td><strong><code>JSON_ERROR_INVALID_PROPERTY_NAME</code></strong></td>
      <td>指定的属性名无法编码。</td>
      <td class="empty">&nbsp;</td>
     </tr>

     <tr>
      <td><strong><code>JSON_ERROR_UTF16</code></strong></td>
      <td>畸形的 UTF-16 字符，可能因为字符编码不正确。</td>
      <td class="empty">&nbsp;</td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.json-last-error-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3419">
    <p><strong>示例 #1 <span class="function"><strong>json_last_error()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;一个有效的&nbsp;json&nbsp;字符串<br /></span><span style="color: #0000BB">$json</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #DD0000">'{"Organization":&nbsp;"PHP&nbsp;Documentation&nbsp;Team"}'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;一个无效的&nbsp;json&nbsp;字符串会导致一个语法错误，在这个例子里我们使用&nbsp;'&nbsp;代替了&nbsp;"&nbsp;作为引号<br /></span><span style="color: #0000BB">$json</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #DD0000">"{'Organization':&nbsp;'PHP&nbsp;Documentation&nbsp;Team'}"</span><span style="color: #007700">;<br /><br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$json&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$string</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Decoding:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$string</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;switch&nbsp;(</span><span style="color: #0000BB">json_last_error</span><span style="color: #007700">())&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">JSON_ERROR_NONE</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'&nbsp;-&nbsp;No&nbsp;errors'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">JSON_ERROR_DEPTH</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'&nbsp;-&nbsp;Maximum&nbsp;stack&nbsp;depth&nbsp;exceeded'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">JSON_ERROR_STATE_MISMATCH</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'&nbsp;-&nbsp;Underflow&nbsp;or&nbsp;the&nbsp;modes&nbsp;mismatch'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">JSON_ERROR_CTRL_CHAR</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'&nbsp;-&nbsp;Unexpected&nbsp;control&nbsp;character&nbsp;found'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">JSON_ERROR_SYNTAX</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'&nbsp;-&nbsp;Syntax&nbsp;error,&nbsp;malformed&nbsp;JSON'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">JSON_ERROR_UTF8</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'&nbsp;-&nbsp;Malformed&nbsp;UTF-8&nbsp;characters,&nbsp;possibly&nbsp;incorrectly&nbsp;encoded'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'&nbsp;-&nbsp;Unknown&nbsp;error'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Decoding: {&quot;Organization&quot;: &quot;PHP Documentation Team&quot;} - No errors
Decoding: {&#039;Organization&#039;: &#039;PHP Documentation Team&#039;} - Syntax error, malformed JSON
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-3420">
    <p><strong>示例 #2 <span class="function"><a href="function.json-encode.html" class="function">json_encode()</a></span> 的 <span class="function"><strong>json_last_error()</strong></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;无效的&nbsp;UTF8&nbsp;序列<br /></span><span style="color: #0000BB">$text&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"\xB1\x31"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$json&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$error&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">json_last_error</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$error&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">JSON_ERROR_UTF8</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
string(4) &quot;null&quot;
bool(true)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-3421">
    <p><strong>示例 #3 <span class="function"><strong>json_last_error()</strong></span> 和 <strong><code>JSON_THROW_ON_ERROR</code></strong></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;导致&nbsp;JSON_ERROR_UTF8&nbsp;的无效&nbsp;UTF8&nbsp;序列<br /></span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #DD0000">"\xB1\x31"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;以下不会导致&nbsp;JSON&nbsp;错误<br /></span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #DD0000">'okay'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">JSON_THROW_ON_ERROR</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;前者的&nbsp;json_encode()&nbsp;不会改变全局错误状态<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">json_last_error</span><span style="color: #007700">()&nbsp;===&nbsp;</span><span style="color: #0000BB">JSON_ERROR_UTF8</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
bool(true)
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.json-last-error-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.json-last-error-msg.html" class="function" rel="rdfs-seeAlso">json_last_error_msg()</a> - 返回最后一次调用 json_encode() 或 json_decode() 时产生的错误信息</span></li>
    <li class="member"><span class="function"><a href="function.json-decode.html" class="function" rel="rdfs-seeAlso">json_decode()</a> - 对 JSON 格式的字符串进行解码</span></li>
    <li class="member"><span class="function"><a href="function.json-encode.html" class="function" rel="rdfs-seeAlso">json_encode()</a> - 对变量进行 JSON 编码</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>